package com.surelution.lansao.wxmp.partner

import java.util.List;

import org.codehaus.groovy.grails.commons.ConfigurationHolder;

import com.surelution.lansao.Partner;
import com.surelution.lansao.PartnerRequiredOperation;
import com.surelution.lansao.wxmp.BaseLansaoAction
import com.surelution.whistle.core.Attribute;

class IDCardPhotoRequiredSub extends RequiredBaseSub {

	@Override
	public boolean accept() {
		if(host.partner) {
			return host.getParam(Attribute.KEY_MsgType) == "image"
		}
		return false;
	}

	@Override
	public List<Attribute> process() {
		def idCardPath = UUID.randomUUID().toString()
		Partner p = Partner.get(host.partner.id)
		p.idCardPhotoFilePath = idCardPath
		File file = new File("${ConfigurationHolder.config.idCardPhotoFileBasePath}${idCardPath}")
		String picUrl = host.getParam("PicUrl")
		URL url = new URL(picUrl)
		file.withOutputStream {os->
			os << url.openStream()
		}
		operation.delete()
		return [new Attribute(Attribute.KEY_Content,"我们收到了您的身份证照片")]
	}

}
